package com.bxw.forum.exception;


import com.bxw.forum.common.AppResult;
import com.bxw.forum.common.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {


    @ResponseBody
    @ExceptionHandler
    public AppResult applicationException (ApplicationException e) {
        //后台异常信息
        e.printStackTrace();
        //日志打印信息
        log.error(e.getMessage());
        //非空校验
        if(e.getErrorResult() != null) {
            return e.getErrorResult();
        }
        if(e.getErrorResult() == null || e.getMessage().equals("")) {
            return AppResult.fail(ResultCode.ERROR_SERVICES);
        }
        //保底返回
        return AppResult.fail(e.getMessage());
    }

    @ResponseBody
    @ExceptionHandler
    public AppResult exceptionHandler(Exception e) {
        //后台异常信息
        e.printStackTrace();
        //日志打印信息
        log.error(e.getMessage());
        //非空校验
        if(e.getMessage() == null || e.getMessage().equals("")) {
            return AppResult.fail(ResultCode.ERROR_SERVICES);
        }
        //保底返回
        return AppResult.fail(e.getMessage());
    }

}
